﻿package com.util.asyncPost;

import android.os.Handler;
import android.os.Message;

import com.google.gson.JsonElement;

class AsyncHandler extends Handler {

	public static final int HTTP_MSG_FAIL = 0;
	public static final int HTTP_MSG_EXCEPTION = 1;
	public static final int HTTP_MSG_SUCCESS = 2;

	private BaseRequest mBaseRequest;

	public AsyncHandler(BaseRequest request) {
		mBaseRequest = request;
	}

	@Override
	public void handleMessage(Message msg) {
		int what = msg.what;
		switch (what) {
		case HTTP_MSG_SUCCESS:
			if (msg.obj != null)
				mBaseRequest.onSuccess((JsonElement) msg.obj);
			else
				mBaseRequest.onSuccess(null);
			break;
		case HTTP_MSG_FAIL:
			mBaseRequest.onFail((String) msg.obj);
			break;
		case HTTP_MSG_EXCEPTION:
			mBaseRequest.onFail(((Exception) msg.obj).toString());
			break;
		default:
			break;
		}
	}
}